package com.example.batch.config;

import com.example.batch.constant.JobConstant;
import com.example.batch.dto.IdDto;
import com.example.batch.process.MyItemProcessor;
import com.example.batch.reader.MyFlatFileReader;
import com.example.batch.writer.MyFlatFileItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;


@Configuration
public class MyBatchConfig extends BaseConfig {


    final MyItemProcessor processor;
    final MyFlatFileReader reader;
    final MyFlatFileItemWriter writer;

    @Autowired
    public MyBatchConfig(MyItemProcessor processor,MyFlatFileReader reader,MyFlatFileItemWriter writer) {
        this.processor = processor;
        this.reader = reader;
        this.writer = writer;
    }

    @Bean
    public Step stepDemo() {
        TaskletStep stepDemo =
                stepBuilderFactory
                        .get("stepDemo")
                        .<IdDto, IdDto>chunk(10)
                        .reader(reader)
                        .processor(processor)
                        .writer(writer)
                        .taskExecutor(new SimpleAsyncTaskExecutor("spring_batch"))
                        .build();
        return stepDemo;
    }


    @Bean
    public Job jobDemo() {
        return jobBuilderFactory.get(JobConstant.SIMPLE_JOB_DEMO)
                .incrementer(new RunIdIncrementer())
                .flow(stepDemo())
                .build().build();
    }


}
